<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>tracing plugin | 微观世界</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/logo.jpg">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="计算机软件 + 物联网 + 云计算项目开发、设计、咨询">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.55f00579.css" as="style"><link rel="preload" href="/assets/js/app.92627f89.js" as="script"><link rel="preload" href="/assets/js/2.817fcd47.js" as="script"><link rel="preload" href="/assets/js/22.18d546b5.js" as="script"><link rel="prefetch" href="/assets/js/10.791ab255.js"><link rel="prefetch" href="/assets/js/11.1dc01403.js"><link rel="prefetch" href="/assets/js/12.c421b677.js"><link rel="prefetch" href="/assets/js/13.8fa15714.js"><link rel="prefetch" href="/assets/js/14.2cca4cf4.js"><link rel="prefetch" href="/assets/js/15.e9f5696c.js"><link rel="prefetch" href="/assets/js/16.62cab094.js"><link rel="prefetch" href="/assets/js/17.6c789b79.js"><link rel="prefetch" href="/assets/js/18.74f9e612.js"><link rel="prefetch" href="/assets/js/19.98ca7c32.js"><link rel="prefetch" href="/assets/js/20.5052a859.js"><link rel="prefetch" href="/assets/js/21.3ea5972a.js"><link rel="prefetch" href="/assets/js/23.069cf840.js"><link rel="prefetch" href="/assets/js/3.62c9d964.js"><link rel="prefetch" href="/assets/js/4.58e718d2.js"><link rel="prefetch" href="/assets/js/5.addf51f6.js"><link rel="prefetch" href="/assets/js/6.a0e5b372.js"><link rel="prefetch" href="/assets/js/7.5d4ccb0f.js"><link rel="prefetch" href="/assets/js/8.ad529520.js"><link rel="prefetch" href="/assets/js/9.cc29df21.js">
    <link rel="stylesheet" href="/assets/css/0.styles.55f00579.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">微观世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="成功案例" class="dropdown-title"><span class="title">成功案例</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/product/smart-home/" class="nav-link">
  智能家居
</a></li><li class="dropdown-item"><!----> <a href="/product/share-ark/" class="nav-link">
  共享快递柜
</a></li><li class="dropdown-item"><!----> <a href="/product/share-washing/" class="nav-link">
  共享洗衣机
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开源项目" class="dropdown-title"><span class="title">开源项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ws-cloud/gateway/" class="nav-link router-link-active">
  ws-cloud-gateway
</a></li></ul></div></div><div class="nav-item"><a href="/contact/" class="nav-link">
  联系我们
</a></div><div class="nav-item"><a href="/about/" class="nav-link">
  关于我们
</a></div><div class="nav-item"><a href="/link/" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/" class="sidebar-link">介绍</a></li><li><a href="/ws-cloud/gateway/1.x/getting-started.html" class="sidebar-link">快速上手</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/" class="sidebar-link">介绍</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/dev.html" class="sidebar-link">插件开发</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>官方插件</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/ws-cloud/gateway/1.x/plugin/official/discovery.html" class="sidebar-link">discovery plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/rewrite.html" class="sidebar-link">rewrite plugin</a></li><li><a href="/ws-cloud/gateway/1.x/plugin/official/tracing.html" class="active sidebar-link">tracing plugin</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/ws-cloud/gateway/1.x/plugin/official/tracing.html#说明" class="sidebar-link">说明</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="tracing-plugin"><a href="#tracing-plugin" class="header-anchor">#</a> tracing plugin</h1> <p>链路追踪</p> <blockquote><p>tracing_plugin.lua</p></blockquote> <h2 id="说明"><a href="#说明" class="header-anchor">#</a> 说明</h2> <p>为每个请求生成 trace-id，并放到 header 中，可以在 accesslog 中配置输出。对于java常用的log框架 log4j, logback 可以将trace-id 关联到 mdc 上下文中并输出到stdout，将请求和 stdout 关联起来。也可关联到其他链路跟踪组件中，如：<a href="https://github.com/apache/skywalking" target="_blank" rel="noopener noreferrer">skywalking<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>, <a href="https://github.com/jaegertracing/jaeger/" target="_blank" rel="noopener noreferrer">jaeger<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>, <a href="https://github.com/dianping/cat" target="_blank" rel="noopener noreferrer">cat<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <div class="language-lua extra-class"><pre class="language-lua"><code><span class="token keyword">function</span> _M<span class="token punctuation">.</span><span class="token function">do_in_access</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">local</span> req <span class="token operator">=</span> ngx<span class="token punctuation">.</span>req

    <span class="token keyword">local</span> req_id <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">get_headers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span>const<span class="token punctuation">.</span>HEADER_TRACE_ID<span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> req_id <span class="token keyword">then</span>
        <span class="token keyword">local</span> trace_id <span class="token operator">=</span> <span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;gen trace id: &quot;</span><span class="token punctuation">,</span> trace_id<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">,</span> const<span class="token punctuation">.</span>HEADER_TRACE_ID<span class="token punctuation">)</span>
        req<span class="token punctuation">.</span><span class="token function">set_header</span><span class="token punctuation">(</span>const<span class="token punctuation">.</span>HEADER_TRACE_ID<span class="token punctuation">,</span> trace_id<span class="token punctuation">)</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
</code></pre></div><blockquote><p>conf/common/logformat.conf</p></blockquote> <div class="language-conf extra-class"><pre class="language-text"><code>log_format json_log '{&quot;@timestamp&quot;:&quot;$time_iso8601&quot;,&quot;domain&quot;:&quot;$host&quot;,&quot;method&quot;:&quot;$request_method&quot;,'
    '&quot;upstream-cache-status&quot;:&quot;$upstream_cache_status&quot;,'
    '&quot;uri&quot;:&quot;$uri&quot;,&quot;origin-uri&quot;:&quot;$origin_uri&quot;,&quot;querystring&quot;:&quot;$args&quot;,&quot;protocol&quot;:&quot;$server_protocol&quot;,&quot;httpcode&quot;:$status,'
    '&quot;bytes&quot;:$body_bytes_sent,&quot;referer&quot;:&quot;$http_referer&quot;,&quot;useragent&quot;:&quot;$http_user_agent&quot;,'
    '&quot;xff&quot;:&quot;$http_x_forwarded_for&quot;,&quot;client&quot;:&quot;$remote_addr&quot;,&quot;upstream&quot;:&quot;$upstream_addr&quot;,&quot;trace-id&quot;:&quot;$http_x_trace_id&quot;,'
    '&quot;upstream-status&quot;:&quot;$upstream_status&quot;,&quot;upstream-time&quot;:&quot;$upstream_response_time&quot;,&quot;response-time&quot;:$request_time,&quot;service&quot;:&quot;$target_service_name&quot;}';

</code></pre></div><blockquote><p>access.log</p></blockquote> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;@timestamp&quot;</span><span class="token operator">:</span> <span class="token string">&quot;2020-05-10T22:39:25+08:00&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;domain&quot;</span><span class="token operator">:</span> <span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;method&quot;</span><span class="token operator">:</span> <span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;upstream-cache-status&quot;</span><span class="token operator">:</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;uri&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/openapi/a&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;origin-uri&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/openapi/demo1/a&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;querystring&quot;</span><span class="token operator">:</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;protocol&quot;</span><span class="token operator">:</span> <span class="token string">&quot;HTTP/1.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;httpcode&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;bytes&quot;</span><span class="token operator">:</span> <span class="token number">39</span><span class="token punctuation">,</span>
    <span class="token property">&quot;referer&quot;</span><span class="token operator">:</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;useragent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;curl/7.64.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;xff&quot;</span><span class="token operator">:</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;client&quot;</span><span class="token operator">:</span> <span class="token string">&quot;127.0.0.1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;upstream&quot;</span><span class="token operator">:</span> <span class="token string">&quot;127.0.0.1:1025&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;trace-id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;42d3d64c-3f13-43fb-b1d7-8dab61b270ce&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;upstream-status&quot;</span><span class="token operator">:</span> <span class="token string">&quot;200&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;upstream-time&quot;</span><span class="token operator">:</span> <span class="token string">&quot;0.002&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;response-time&quot;</span><span class="token operator">:</span> <span class="token number">0.002</span><span class="token punctuation">,</span>
    <span class="token property">&quot;service&quot;</span><span class="token operator">:</span> <span class="token string">&quot;demo1&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/ws-cloud/gateway/1.x/plugin/official/rewrite.html" class="prev">
        rewrite plugin
      </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.92627f89.js" defer></script><script src="/assets/js/2.817fcd47.js" defer></script><script src="/assets/js/22.18d546b5.js" defer></script>
  </body>
</html>
